import {defineStore} from 'pinia'
import {ref} from "vue";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {UserControllerService, UserLoginVO} from "../../generated";

export const useUserStore = defineStore(
    'user',
    () => {
        const loginUser = ref<UserLoginVO>({})
        const isLogin = ref(false)

        let router = useRouter();

        const login = async (account: string, password: string) => {
            let res = await UserControllerService.loginUsingPost({
                account: account,
                password: password
            });
            if (res.code === 0) {
                ElMessage({
                    message: '登录成功',
                    type: 'success',
                })
                loginUser.value = res.data
                isLogin.value = true
                await router.push("/problem")
            } else {
                ElMessage.error(res.message)
            }
        }

        const register = async (account: string, password: string, email: string) => {
            let res = await UserControllerService.registerUsingPost({
                account: account,
                password: password,
                email: email
            });
            if (res.code === 0) {
                ElMessage.success("注册成功");
                loginUser.value = res.data;
                isLogin.value = true;
                await router.push("/problem")
            } else {
                ElMessage.error(res.message)
            }
        }

        const checkIsLogin = async () => {
            // 检查登录状态
            const loginCheckRes = await UserControllerService.checkLoginUsingGet();
            if (loginCheckRes.code !== 0) {
                // API 调用失败
                return;
            }
            
            // 用户未登录
            if (!loginCheckRes.data) {
                loginUser.value = {};
                isLogin.value = false;
                return;
            }
            
            // 用户已登录但本地没有用户信息，需要获取用户信息
            if (!loginUser.value.account) {
                const userInfoRes = await UserControllerService.getUserInfoUsingGet();
                if (userInfoRes.code === 0 && userInfoRes.data) {
                    loginUser.value = userInfoRes.data;
                    isLogin.value = true;
                } else {
                    ElMessage.error(userInfoRes.message);
                }
            }
        }

        const logout = async () => {
            let res = await UserControllerService.logoutUsingPost();
            if (res.code === 0) {
                ElMessage.success("登出成功")
                loginUser.value = {};
                isLogin.value = false;
            } else {
                ElMessage.error(res.message)
            }
        }

        return {
            loginUser,
            isLogin,
            login,
            register,
            checkIsLogin,
            logout
        }

    },
    {
        persist: true
    }
);
